package com.jgsu.farm.controller;

import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.DiseaseStressService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/stresscategory/diseasestress", tags = "病号胁迫类型信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/stresscategory/diseasestress")
public class DiseaseStressController {
    @Autowired
    DiseaseStressService diseaseStressService;

    /**
     * 病害胁迫增加
     * */
    @ApiOperation(value = "病害胁迫添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult diseaseStressAdd(DiseaseStress diseaseStress) {
        try {
            diseaseStressService.add(diseaseStress);
            log.info("病害胁迫添加成功，病害胁迫chineseName为：{}", diseaseStress.getChinesename());
            return CommonResult.success(diseaseStress);
        } catch (DataAddException e) {
            log.error("病害胁迫添加失败，病害胁迫chineseName为：{}", diseaseStress.getChinesename());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }


    /**
     * 病害胁迫删除
     *  */
    @ApiOperation(value = "病害胁迫删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult diseaseStressDelete (@RequestParam("diseaseid") Integer diseaseid){
        try {
            diseaseStressService.delete(diseaseid);
            log.info("成功删除diseaseId为:{}",diseaseid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException dde) {
            log.error("删除diseaseId为:{}的数据失败",diseaseid);
            throw new DataNotExistException(dde.getCode(), dde.getMessage());
        }
    }

    /**
     * 病害胁迫查询
     */
    @ApiOperation(value = "病害胁迫查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<DiseaseStress>> diseaseStressList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                                   @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
            try {
                PageInfo<DiseaseStress> diseasestressList = diseaseStressService.selectAll(page, pagesize);
                log.info("查询所有的病害胁迫信息");
                return CommonResult.success(diseasestressList);
            } catch (DataNotExistException e) {
                log.error("病害胁迫信息表中没有数据");
                throw new DataNotExistException(e.getCode(), e.getMessage());
            }

        }

    /**
     * 病害胁迫修改
     */
    @ApiOperation(value = "病害胁迫修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult diseaseStressUpdate(DiseaseStress diseasestress) {
        try {
            diseaseStressService.update(diseasestress);
            log.info("更新chineseName为{}的病害胁迫成功", diseasestress.getChinesename());
            return CommonResult.success(diseasestress);
        } catch (DataNotExistException e) {
            log.error("更新chineseName为{}的病害胁迫失败", diseasestress.getChinesename());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
